# -*- coding: utf-8 -*-
import subprocess

import conf
from conf import WxConfig, ZfbConfig, BdConfig, KAppConfig
from utils.common import CrawlerLogger, str2bool, uninstall_appium_app
from task import Task
import argparse
from utils.common import init_logging


def start_mitmproxy(file_name):
    # 设定mitmweb的参数
    command = [
        "mitmweb",
        "--mode", "upstream:http://127.0.0.1:10809",
        "-s", f"./database/mitmproxy_handler.py --file-name {file_name}",  # 使用特定标记传入 file_name
        "-p", "8899",
        "-q",
        "-k"
    ]

    # 启动mitmproxy
    subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)


def main(applets_name):
    CrawlerLogger.info(f"[*] Loading config. micro program: {conf.CLIENT_SIDE_CONF.PLATFORM}. "
                       f"selection: {conf.CLIENT_SIDE_CONF.SELECTION_MODE}. "
                       f"webview: {conf.WEBVIEW_MODE}. "
                       f"device_id: {conf.CLIENT_SIDE_CONF.DEVICE_ID}. "
                       f"applet_name: {applets_name}")
    task = Task(applets_name)
    task.run()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='A powerful Lightweight Test Generator for WeChat Mini Programs',
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--platform", "-p", metavar="wx", type=str, nargs="?",
                        help="The Small program platform", dest="platform", required=True)
    parser.add_argument("--webview", "-w", metavar="True", type=str2bool, nargs="?",
                        help="The exploration mode", dest="webview_mode", required=True)
    parser.add_argument("--applets-name", "-u", metavar="国务院", type=str, nargs="?",
                        help="The target applets", dest="applets_name", required=True)
    parser.add_argument("--log-dictionary-name", "-l", metavar="logs", type=str, nargs="?",
                        help="The log dictionary name", dest="log_dictionary_name", required=True)
    parser.add_argument("--device-id", "-d", metavar="8ABY0H7HM", type=str, nargs="?",
                        help="The device id", dest="device_id", required=True)
    parser.add_argument("--log-level", choices=["debug", "info"], type=str, nargs="?", default="info",
                        help="Log level of crawler. Default: info", dest="log_level", required=False)
    args = parser.parse_args()

    applets_name = args.applets_name
    platform = args.platform
    webview_mode = args.webview_mode
    device_id = args.device_id
    log_dictionary_name = args.log_dictionary_name

    # start_mitmproxy(applets_name)

    if platform.lower() == "wx":
        conf.CLIENT_SIDE_CONF = WxConfig()
    elif platform.lower() == 'zfb':
        conf.CLIENT_SIDE_CONF = ZfbConfig()
    elif platform.lower() == 'bd':
        conf.CLIENT_SIDE_CONF = BdConfig()
    elif platform.lower() == 'kuai':
        conf.CLIENT_SIDE_CONF = KAppConfig()

    if webview_mode:
        conf.WEBVIEW_MODE = True
    else:
        conf.WEBVIEW_MODE = False
    conf.CLIENT_SIDE_CONF.DEVICE_ID = device_id
    conf.LOG_DICTIONARY_NAME = log_dictionary_name

    init_logging(args.log_level, platform.lower(), applets_name)
    if conf.CLIENT_SIDE_CONF.PLATFORM != "kuai":
        uninstall_appium_app()
    main(applets_name)